//
//  SYDFeedbackController.m
//  PinGJByOwnerSide
//
//  Created by lantaiyuan on 2017/8/12.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

#import "SYDFeedbackController.h"
#import "SYDFeedBackHeaderView.h"
#import "YMAddPhotoView.h"
#import "SYDFeedBackFootView.h"
#import "SYDPlaceholderView.h"
@interface SYDFeedbackController ()<UIScrollViewDelegate,UITextViewDelegate,UITextFieldDelegate>
@property(nonatomic,strong)UIScrollView *contentScrollView;
@property(nonatomic,strong)UIButton *SubmitFeedbackBtn;
@property(nonatomic,strong)SYDFeedBackHeaderView *headerView;
@property (nonatomic, strong) YMAddPhotoView * photoView;

@property(nonatomic,strong)SYDFeedBackFootView *feedBackFootView;
//提示语
@property(nonatomic,strong)SYDPlaceholderView *holderView;
@property(nonatomic,strong)NSMutableArray *selectedPhotos;//选择

//二进制数据流
@property (nonatomic, strong) NSMutableArray *commentDatas;

//问题属性
@property(nonatomic,assign)NSInteger problemType;

@end

@implementation SYDFeedbackController
#pragma mark - 导航栏
- (void)navigationBarHandle{
    
    WEAKSELF
    [weakSelf backButtonAction:^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //偏移20/64适配
    if (@available(iOS 11.0,*)) {
        self.contentScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;//UIScrollView也适用
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.navigationBar.title = @"意见反馈";
    [self navigationBarHandle];
    [self initFeedBackUI];
}
-(void)initFeedBackUI{
    if (iPhone8_X) {
        _SubmitFeedbackBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 58, SCREEN_WIDTH, 58)];
    } else {
        _SubmitFeedbackBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 48, SCREEN_WIDTH, 48)];
    }
    [_SubmitFeedbackBtn setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
    _SubmitFeedbackBtn.backgroundColor = GREEN_COLOR;
    _SubmitFeedbackBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    [_SubmitFeedbackBtn setTitle:@"提交反馈" forState:UIControlStateNormal];
    [self.view addSubview:self.SubmitFeedbackBtn];
    [self.contentScrollView addSubview:self.headerView];
    
    [self.contentScrollView addSubview:self.holderView];
    [self.contentScrollView addSubview:self.photoView];
    [self.contentScrollView addSubview:self.feedBackFootView];
    WEAKSELF
    self.headerView.block = ^(NSInteger tag) {
        weakSelf.problemType = tag;
    };
    
    // set default selected
    [self.headerView setDefaultSelect:function];
   
  [[self.SubmitFeedbackBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
      weakSelf.selectedPhotos = (NSMutableArray *)[weakSelf.photoView getThumbnailImgArray];
      NSLog(@"%ld---%@---%@",weakSelf.problemType,weakSelf.holderView.text,weakSelf.feedBackFootView.contactField.text);
      if (!(weakSelf.problemType > 0)) {//没有填问题属性
          [SVProgressHUD displayInfoWithStatus:@"请填写问题属性" time:1.5];
      }else if ([weakSelf.holderView.text isEqualToString:@""]){
          [SVProgressHUD displayInfoWithStatus:@"请填写反馈内容" time:1.2];
      }else if (!(weakSelf.feedBackFootView.contactField.text.length > 0)){
          [SVProgressHUD displayInfoWithStatus:@"请填写联系方式" time:1.2];
      }else{
        [weakSelf mineSuggestAddtion];
      }
  }];
}
#pragma mark---添加意见反馈
 
-(void)mineSuggestAddtion{
    WEAKSELF
    for (NSInteger i = 0; i < self.selectedPhotos.count; i++) {
        [weakSelf.commentDatas addObject:UIImageJPEGRepresentation(weakSelf.selectedPhotos[i], 0.01)];
    }
    NSDictionary *dict = @{
                           @"userId":[YMSaveTool valueForKey:USER_ID],
                           @"problemType":[NSString stringWithFormat:@"%ld",weakSelf.problemType],
                           @"feedbackContent":weakSelf.holderView.text,
                           @"content":self.holderView.text,
                           @"contactChannel":weakSelf.feedBackFootView.contactField.text,
                           @"cityCode":[YMSaveTool valueForKey:CityCode]
                           };
    [[SYDNetwork shareTool]uploadMostMultipartFilesWithParameters:dict mFile:@"mFile" uploadDatas:self.selectedPhotos success:^(NSURLSessionDataTask *task, id responseObject) {
        @try {
            NSLog(@"-----%@",responseObject);
            if ([responseObject[@"statusCode"]integerValue] == 200) {
                [self.selectedPhotos removeAllObjects];
                [UIView animateWithDuration:1.0 animations:^{
                    [SVProgressHUD displayInfoWithStatus:@"您的意见已经收到,我们会尽快进行核实" time:1.0];
                } completion:^(BOOL finished) {
                    [weakSelf.navigationController popViewControllerAnimated:YES];
                }];
            }
        } @catch (NSException *exception) {
            NSLog(@"---%@",exception.description);
        } @finally {
            
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD displayInfoWithStatus:@"请检查网络..." time:1.0];
    }];
}
#pragma mark---图片转化
-(NSData *)image_TransForm_Data:(UIImage *)image{
    NSData *data = UIImageJPEGRepresentation(image, 0.5);
    return data;
}
#pragma mark---UITextViewDelegate
//把回车键当做退出键盘的响应键  textView退出键盘的操作
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    
    if ([@"\n" isEqualToString:text] == YES)
    {
        [textView resignFirstResponder];
        return NO;
    }
    
    return YES;
}

#pragma mark---Lazy
-(UIScrollView *)contentScrollView{
    if (!_contentScrollView) {
        if (iPhone8_X) {
            _contentScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 88, SCREEN_WIDTH, SCREEN_HEIGHT - 88 - 58)];
        } else {
            _contentScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 48)];
        }
        _contentScrollView.backgroundColor = CONTENT_BG_COLOR;
        _contentScrollView.showsVerticalScrollIndicator = NO;
        _contentScrollView.showsHorizontalScrollIndicator = NO;
        _contentScrollView.delegate = self;
        _contentScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, CGRectGetMaxY(self.feedBackFootView.frame));
        _contentScrollView.scrollEnabled = YES;
        [self.view addSubview:_contentScrollView];
    }
    return _contentScrollView;
}
-(SYDFeedBackHeaderView *)headerView{
    if (!_headerView) {
        _headerView = [[SYDFeedBackHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 144)];
    }
    return _headerView;
}
-(SYDPlaceholderView *)holderView{
    
    if (_holderView == nil) {
        CGFloat y = CGRectGetMaxY(self.headerView.frame);
        _holderView = [[SYDPlaceholderView alloc]initWithFrame:CGRectMake(0, y, SCREEN_WIDTH, 101)];
        _holderView.delegate = self;
        _holderView.font = [UIFont systemFontOfSize:12];
        _holderView.textColor = SUB_TITLE_COLOR;
        _holderView.textAlignment = NSTextAlignmentLeft;
        _holderView.editable = YES;
        _holderView.placeHolderColor = GRAY_COLOR;
        _holderView.placeHolder = @"请输入您的意见或者感受，我们将不断改进。";
        [_holderView addTopLineWithColor:[UIColor colorWithHexString:@"#e8e8e8"]];
    }
    return _holderView;
}
-(NSMutableArray *)selectedPhotos{
    
    if (!_selectedPhotos) {
        _selectedPhotos = [NSMutableArray array];
    }
    return _selectedPhotos;
}
-(NSMutableArray *)commentDatas{
    if (!_commentDatas) {
        _commentDatas = [NSMutableArray array];
    }
    return _commentDatas;
}
- (UIView *)photoView{
    if (!_photoView) {
        _photoView = [[YMAddPhotoView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.holderView.frame), SCREEN_WIDTH, 84)];
    }
    return _photoView;
}
-(SYDFeedBackFootView *)feedBackFootView{
    if (!_feedBackFootView) {
        _feedBackFootView = [[SYDFeedBackFootView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.photoView.frame), SCREEN_WIDTH, 88)];
    }
    return _feedBackFootView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

